<?php


namespace Common\Model;


use Org\Util\Base;

class CategoryModel extends BaseModel
{
    protected $pk='cid';
    protected $tableName='category';
    protected $_validate=[
        ['cname','require','请填写分类名称信息'],
        ['rank','require','请填写网站配置信息'],
        ['pid','require','请填写网站配置信息'],

    ];



    public function getCateData($cid)
    {
        //p(Db::table('category')->get());
        //p($cid);
        //1.找当前数据的子集
        $data = m('category')->select();
        $cids = $this->getSon($data,$cid);
        //p($cids);
        //2.把自己追加进去
        $cids[] = $cid;
        //3.找到除了自己和自己子集之外的数据

        $map['cid']=array('not in',$cids);
        $data = $this->where($map)->select();
        return (new Base())->tree($data,'cname');

//        p($data);
    }


    public function getSon($data,$cid)
    {
        static $temp = [];
        foreach($data as $k=>$v)
        {
            if($v['pid']==$cid){
                $temp[] = $v['cid'];
                $this->getSon($data,$v['cid']);
            }
        }
        return $temp;
    }

}